package com.hengxinguotong.zhihuichengjian.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.bean.Material;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by liux on 2017/3/7.
 */

public class MaterialCanUseListAdapter extends BaseAdapter {
    private List<Material> datas;
    private Context context;
    private int select = -1;

    public MaterialCanUseListAdapter(List<Material> datas, Context context) {
        this.context = context;
        this.datas = datas;

    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null || convertView.getTag() == null) {
            convertView = LayoutInflater.from(context)
                    .inflate(R.layout.item_material_use, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Material material = datas.get(position);
        holder.model.setText(material.getModel());
        holder.name.setText(material.getName());
        holder.manufacturer.setText(material.getManufacturer());
        holder.units.setText(material.getAvailableQuantity()+material.getUnit());
        if (position == select) {
            holder.checkbox.setImageResource(R.mipmap.circle_choice_ligt);
        } else {
            holder.checkbox.setImageResource(R.mipmap.circle_choice);
        }
        return convertView;
    }

    public Material getSelect() {
        if(select==-1){
            return null;
        }
        return datas.get(select);
    }

    public void setSelect(int select) {
        this.select = select;
        notifyDataSetChanged();
    }

    static class ViewHolder {
        @BindView(R.id.name)
        HXTextView name;
        @BindView(R.id.units)
        HXTextView units;
        @BindView(R.id.model)
        HXTextView model;
        @BindView(R.id.manufacturer)
        HXTextView manufacturer;
        @BindView(R.id.checkbox)
        ImageView checkbox;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
